<?php

namespace app\model;

use think\model\concern\SoftDelete;

class CategoryModel extends BaseModel
{
    protected $name = 'category';

    use SoftDelete;

    protected $deleteTime = "deleted_at";

    public const TYPE_MATERIAL = 'material'; //物料
    public const TYPE_PRODUCT = 'product'; //产品

    public const TYPE_ARRAY = [
        self::TYPE_MATERIAL,
        self::TYPE_PRODUCT
    ];


    /**
     * Notes: 添加分类
     * User: chenLei
     * Date: 9/28/25
     * @param $params
     * @return true
     */
    public function add($params)
    {
        $this->save([
            'name' => $params['name'],
            'type' => $params['type'],
            'pid' => $params['pid'] ?? 0,
            'image' => $params['image'] ?? '',
            'company_id' => $params['company_id'],
        ]);

        return true;
    }


    /**
     * Notes: 编辑
     * User: chenLei
     * Date: 9/28/25
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit($params)
    {
        $info = $this->where('id', $params['id'])->find();
        if (empty($info)) {
            $this->error = '分类不存在';
            return false;
        }

        if (!empty($params['pid']) && $params['pid'] != 0) {
            if ($params['pid'] == $info['id']) {
                $this->error = '父级分类不能是自己';
                return false;
            }
        }

        $info->save([
            'name' => $params['name'],
            'pid' => $params['pid'] ?? 0,
            'image' => $params['image'] ?? '',
        ]);

        return true;
    }


    /**
     * Notes: 删除分类
     * User: chenLei
     * Date: 9/28/25
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del($params)
    {
        $info = $this->where('id', $params['id'])->find();
        if (empty($info)) {
            $this->error = '分类不存在';
            return false;
        }

        $info->delete();
        return true;
    }


    /**
     * Notes: 分页列表
     * User: chenLei
     * Date: 9/28/25
     * @param $params
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function list($params)
    {
        $where = [];
        $user = getUserInfo();
        $dataRole = dataPermissionCheck($user);
        if (in_array($dataRole, ['user', 'company'])) {
            $where[] = ['company_id', '=', $user['company_id']];
        }
        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        if (!empty($params['type'])) {
            $where[] = ['type', '=', $params['type']];
        }

        return $this->where($where)->order('id desc')->paginate($params['page_size'] ?? 15);
    }



    public function treeList($params)
    {
        $where = [];
        $where[] = ['type','=',$params['type']];
        $user = getUserInfo();
        $dataRole = dataPermissionCheck($user);
        if (in_array($dataRole, ['user', 'company'])) {
            $where[] = ['company_id', '=', $user['company_id']];
        }

        $list = $this->where('pid', 0)->where($where)->select();

        foreach ($list as $key => $value) {
            $list[$key]['child'] = $this->where('pid', $value['id'])->select();
        }

        return $list;
    }
}